package com.cqjtu.store.favorite.controller;

import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.PageVO;
import com.cqjtu.store.common.R;
import com.cqjtu.store.favorite.pojo.dao.FavoritePageDTO;
import com.cqjtu.store.favorite.service.FavoriteService;
import com.cqjtu.store.product.pojo.vo.ProductVO;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ProjectName: cqjtu-store
 * @Titile: FavoriteController
 * @Author: Lucky
 * @Description: 收藏控制类
 */
@RestController
@RequestMapping("api/favorite")
@Api(tags = "收藏模块")
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;

    /**
     * 查询收藏列表
     * @param favoritePageDTO
     * @param request
     * @return
     */
    @GetMapping("list")
    @ApiOperation("查询收藏列表")
    public R<PageVO<List<ProductVO>>> getFavoriteList(@RequestBody @Validated FavoritePageDTO favoritePageDTO,
                                                      HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        //调用业务层
        PageVO<List<ProductVO>> favoriteList = favoriteService.getFavoriteList(favoritePageDTO, userLoginVO);
        return R.ok(favoriteList);
    }

    /**
     * 添加收藏
     * @param productId
     * @param request
     * @return
     */
    @PostMapping("add/{productId}")
    @ApiOperation("添加收藏")
    public R<Void> addFavorite(@PathVariable("productId") Integer productId,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        favoriteService.addFavorite(productId,userLoginVO);
        return R.ok();
    }

    /**
     * 取消收藏
     * @param productId
     * @param request
     * @return
     */
    @DeleteMapping("cancel/{productId}")
    @ApiOperation("取消收藏")
    public R<Void> cancelFavorite(@PathVariable("productId") Integer productId,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        favoriteService.cancelFavorite(productId,userLoginVO);
        return R.ok();
    }
}
